package com.zzw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzw.bean.Menu;
import com.zzw.bean.Msg;
import com.zzw.service.MenuService;
import com.zzw.utils.RedisUtil;
import com.zzw.utils.ShiroUtils;
import com.zzw.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 * 提供菜单管理与菜单树相关功能
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Api(tags = "菜单管理接口")
@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 菜单管理页面
     */
    @ApiOperation(value = "菜单管理页面", notes = "跳转到菜单管理页面")
    @RequestMapping("list")
    public String list() {
        return "menu/list";
    }

    /**
     * 菜单Demo页面
     */
    @ApiOperation(value = "菜单Demo页面", notes = "跳转到菜单Demo页面")
    @RequestMapping("demo")
    public String demo() {
        return "menu/demo";
    }

    /**
     * 根据条件分页查询根节点菜单
     */
    @ApiOperation(value = "查询根节点菜单", notes = "根据条件分页查询根节点菜单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, dataType = "Integer", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer", paramType = "query", defaultValue = "10"),
        @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序方式", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping("/listRootNodeByCondition")
    @ResponseBody
    public Msg listRootNodeByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "sort", defaultValue = "") String sort,
                                        @RequestParam(value = "order", defaultValue = "") String order,
                                        Menu menu) {

        PageHelper.startPage(pageNum, pageSize, WebUtils.getOrderBy(sort, order));

        List<Menu> menus = menuService.selectMenuRootElement();
        PageInfo pageInfo = new PageInfo(menus, pageSize);

        return Msg.success().add("pageInfo", pageInfo);
    }

    /**
     * 获取所有根节点菜单（不分页）
     * 支持按条件筛选
     */
    @ApiOperation(value = "获取所有根节点菜单", notes = "获取所有根节点菜单，不分页")
    @RequestMapping("/listRootNode")
    @ResponseBody
    public Msg listRootNode() {
        List<Menu> menus = menuService.selectMenuRootElement();
        return Msg.success().add("menus", menus);
    }

    /**
     * 根据父节点ID获取子节点菜单
     */
    @ApiOperation(value = "获取子节点菜单", notes = "根据父节点ID获取子节点菜单")
    @RequestMapping("/listChildNodes")
    @ResponseBody
    public Msg listChildNodes(Menu menu) {
        List<Menu> menus = menuService.selectMenuChildElement(menu);
        return Msg.success().add("menus", menus);
    }

    //菜单管理树表格 使用这个接口
    //懒加载, 每次展开菜单时，都会调用这个接口
    @ApiOperation(value = "查询菜单节点", notes = "菜单管理树表格使用，懒加载菜单节点")
    @RequestMapping("/selectMenuNode")
    @ResponseBody
    public Msg selectMenuNode(Menu menu) {
        List<Menu> menus = menuService.selectMenuNode(menu);
        return Msg.success().add("menus", menus);
    }

    //首页菜单树 使用的是这个接口
    //懒加载, 每次展开菜单时，都会调用这个接口
    @ApiOperation(value = "根据用户ID查询菜单节点", notes = "首页菜单树使用，根据用户权限懒加载菜单节点")
    @RequestMapping("/selectMenuNodeByUserId")
    @ResponseBody
    public Msg selectMenuNodeByUserId(Menu menu) {
        Integer userId = ShiroUtils.getUserId();
        log.info("用户ID：{}", userId);
        List<Menu> menus = menuService.selectMenuNodeByUserId(menu, userId);
        return Msg.success().add("menus", menus);
    }

    /**
     * 根据角色ID查询菜单节点，并标记该角色已拥有的菜单
     * @param menu 菜单信息
     * @param roleId 角色ID
     * @return 菜单节点列表
     */
    @ApiOperation(value = "根据角色ID查询菜单节点", notes = "查询菜单节点并标记角色已拥有的菜单")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping("/selectMenuNodeByRoleId")
    @ResponseBody
    public Msg selectMenuNodeByRoleId(Menu menu, Integer roleId) {
        List<Menu> menus = menuService.selectMenuNodeByRoleId(menu, roleId);
        return Msg.success().add("menus", menus);
    }

    @ApiOperation(value = "添加菜单节点", notes = "添加新的菜单节点")
    @PostMapping("/add")
    @ResponseBody
    public Msg insertNode(@RequestBody Menu menu) {
        menuService.insertNode(menu);
        return Msg.success();
    }

    @ApiOperation(value = "删除菜单节点", notes = "根据ID删除菜单节点")
    @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "Integer", paramType = "query")
    @PostMapping("/delete")
    @ResponseBody
    public Msg deleteNode(Integer id) {
        menuService.deleteNode(id);
        return Msg.success();
    }

    @ApiOperation(value = "更新菜单节点", notes = "更新菜单节点信息")
    @PostMapping("/update")
    @ResponseBody
    public Msg updateNode(@RequestBody Menu menu) {
        menuService.updateNode(menu);
        return Msg.success();
    }
}
